/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.pool.modelo;

import com.isd.inventario.modelo.Producto;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author ctrosch
 */
@Entity
@Table(name = "USR_POOLRI")
@IdClass(ItemReservaPK.class)
public class ItemReserva implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "USR_POOLRI_NRPOOL", nullable = false)
    private int nroPool;
    @Id
    @Column(name = "USR_POOLRI_NROCTA", nullable = false, length = 13)
    private String nroCta;
    @Id
    @Column(name = "USR_POOLRI_NORDEN", nullable = false)
    private int orden;
    @Id
    @Column(name = "USR_POOLRI_TIPPRO", nullable = false, length = 6)
    private String tippro;
    @Id
    @Column(name = "USR_POOLRI_ARTCOD", nullable = false, length = 30)
    private String artcod;

    @JoinColumns({
    @JoinColumn(name = "USR_POOLRI_NRPOOL", referencedColumnName = "USR_POOLRS_POOL_NRPOOL", nullable = false, insertable=false, updatable=false),
    @JoinColumn(name = "USR_POOLRI_NROCTA", referencedColumnName = "USR_POOLRS_NROCTA", nullable = false,insertable=false, updatable=false),
    @JoinColumn(name = "USR_POOLRI_NORDEN", referencedColumnName = "USR_POOLRS_NORDEN", nullable = false, insertable=false, updatable=false)
    })
    @ManyToOne
    private ReservasPool reserva;

    @JoinColumns({
    @JoinColumn(name = "USR_POOLRI_TIPPRO", referencedColumnName = "STMPDH_TIPPRO", nullable = false, insertable=false, updatable=false),
    @JoinColumn(name = "USR_POOLRI_ARTCOD", referencedColumnName = "STMPDH_ARTCOD", nullable = false, insertable=false, updatable=false)
    })
    @ManyToOne
    private Producto producto;

    @Column(name = "USR_POOLRI_CANTID", precision = 18, scale = 4)
    private BigDecimal cantidad;

    @Column(name = "USR_POOLRI_PRECIO", precision = 20, scale = 6)
    private BigDecimal precio;
    
    @Column(name = "USR_PO_FECALT")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fecalt;
    @Column(name = "USR_PO_FECMOD")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fecmod;
    @Column(name = "USR_PO_USERID", length = 15)
    private String userid;
    @Column(name = "USR_PO_ULTOPR")
    private String ultopr;
    @Column(name = "USR_PO_DEBAJA")
    private String debaja;
    @Column(name = "USR_PO_OALIAS", length = 10)
    private String oalias;

    @Transient
    private BigDecimal cantidadMinima;
    @Transient
    private BigDecimal cantidadMaxima;
    @Transient
    private int cantidadDisponible;

    @Transient
    private BigDecimal precioFinalConIVA;

    @Transient
    private BigDecimal precioFinalSinIVA;

    @Transient
    private BigDecimal totalLineaSinIVA;

    @Transient
    private BigDecimal totalLineaConIVA;


    public ItemReserva() {

        this.fecalt = new Date();
        this.fecmod = new Date();
        this.userid = "TIVI";
        this.ultopr = "A";
        this.debaja = "N";
        this.oalias = "USR_POOLRI";
        this.cantidad = BigDecimal.ZERO;

    }

    public ItemReserva(ReservasPool reserva, Producto producto) {

        this.nroPool = reserva.getNroPool();
        this.nroCta = reserva.getNroCuenta();
        //this.orden = reserva.getOrden();
        this.tippro = producto.getTippro();
        this.artcod = producto.getArtcod();

        this.reserva = reserva;
        this.producto = producto;
        this.cantidad = BigDecimal.ZERO;

        this.fecalt = new Date();
        this.fecmod = new Date();
        this.userid = "TIVI";
        this.ultopr = "A";
        this.debaja = "N";
        this.oalias = "USR_POOLRI";
    }

    public ReservasPool getReserva() {
        return reserva;
    }

    public void setReserva(ReservasPool reserva) {
        this.reserva = reserva;
    }

    public String getDebaja() {
        return debaja;
    }

    public void setDebaja(String debaja) {
        this.debaja = debaja;
    }

    public Date getFecalt() {
        return fecalt;
    }

    public void setFecalt(Date fecalt) {
        this.fecalt = fecalt;
    }

    public Date getFecmod() {
        return fecmod;
    }

    public void setFecmod(Date fecmod) {
        this.fecmod = fecmod;
    }

    public String getOalias() {
        return oalias;
    }

    public void setOalias(String oalias) {
        this.oalias = oalias;
    }

    public String getUltopr() {
        return ultopr;
    }

    public void setUltopr(String ultopr) {
        this.ultopr = ultopr;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }
    
    public String getArtcod() {
        return artcod;
    }

    public void setArtcod(String artcod) {
        this.artcod = artcod;
    }

    public BigDecimal getCantidad() {
        return cantidad;
    }

    public void setCantidad(BigDecimal cantidad) {
        this.cantidad = cantidad;
    }

    public String getNroCta() {
        return nroCta;
    }

    public void setNroCta(String nroCta) {
        this.nroCta = nroCta;
    }

    public int getNroPool() {
        return nroPool;
    }

    public void setNroPool(int nroPool) {
        this.nroPool = nroPool;
    }

    public int getOrden() {
        return orden;
    }

    public void setOrden(int orden) {
        this.orden = orden;
    }

    public BigDecimal getPrecio() {
        return precio;
    }

    public void setPrecio(BigDecimal precio) {
        this.precio = precio;
    }

    public String getTippro() {
        return tippro;
    }

    public void setTippro(String tippro) {
        this.tippro = tippro;
    }

    public Producto getProducto() {
        return producto;
    }

    public void setProducto(Producto producto) {
        this.producto = producto;
    }

    public BigDecimal getCantidadMaxima() {
        return cantidadMaxima;
    }

    public void setCantidadMaxima(BigDecimal cantidadMaxima) {
        this.cantidadMaxima = cantidadMaxima;
    }

    public BigDecimal getCantidadMinima() {
        return cantidadMinima;
    }

    public void setCantidadMinima(BigDecimal cantidadMinima) {
        this.cantidadMinima = cantidadMinima;
    }
    
     public BigDecimal getPrecioFinalConIVA() {
        try {
            if (precio != null) {

                precioFinalConIVA = precio;

                if (producto.getCodcpt().equals("V001")) {
                    precioFinalConIVA = precio.multiply(new BigDecimal(1.21));
                }
                if (producto.getCodcpt().equals("V002")) {
                    precioFinalConIVA = precio.multiply(new BigDecimal(1.105));
                }
                if (producto.getCodcpt().equals("V003")) {
                    precioFinalConIVA = precio;
                }
            }

            return precioFinalConIVA;

        } catch (Exception e) {

            return BigDecimal.ZERO;
        }
    }

    public void setPrecioFinalConIVA(BigDecimal precioFinalConIVA) {
        this.precioFinalConIVA = precioFinalConIVA;
    }
    
    public BigDecimal getPrecioFinalSinIVA() {

        try {
            if (precio != null) {
                
                precioFinalSinIVA = precio;
            }

            return precioFinalSinIVA;

        } catch (Exception e) {

            return BigDecimal.ZERO;
        }
    }

    public void setPrecioFinalSinIVA(BigDecimal precioFinalSinIVA) {
        this.precioFinalSinIVA = precioFinalSinIVA;
    }

    public BigDecimal getTotalLineaConIVA() {

        try {
            if (cantidad != null && precio !=null) {

                totalLineaConIVA = cantidad.multiply(precio);

                if(producto!=null){

                    if (producto.getCodcpt().equals("V001")) {
                        totalLineaConIVA = totalLineaConIVA.multiply(new BigDecimal(1.21));
                    }
                    if (producto.getCodcpt().equals("V002")) {
                        totalLineaConIVA = totalLineaConIVA.multiply(new BigDecimal(1.105));
                    }
                    if (producto.getCodcpt().equals("V003")) {
                        totalLineaConIVA = totalLineaConIVA;
                    }
                }
            }

            return totalLineaConIVA;

        } catch (Exception e) {

            return BigDecimal.ZERO;
        }
    }

    public void setTotalLineaConIVA(BigDecimal totalLineaConIVA) {
        this.totalLineaConIVA = totalLineaConIVA;
    }

    public BigDecimal getTotalLineaSinIVA() {

        try {
            totalLineaSinIVA = BigDecimal.ZERO;

            if (cantidad != null && precio !=null) {
                
                totalLineaSinIVA = cantidad.multiply(precio);
            }
            return totalLineaSinIVA;
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public void setTotalLineaSinIVA(BigDecimal totalLineaSinIVA) {
        this.totalLineaSinIVA = totalLineaSinIVA;
    }
    
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ItemReserva other = (ItemReserva) obj;
        if (this.nroPool != other.nroPool) {
            return false;
        }
        if ((this.nroCta == null) ? (other.nroCta != null) : !this.nroCta.equals(other.nroCta)) {
            return false;
        }
        if (this.orden != other.orden) {
            return false;
        }
        if ((this.tippro == null) ? (other.tippro != null) : !this.tippro.equals(other.tippro)) {
            return false;
        }
        if ((this.artcod == null) ? (other.artcod != null) : !this.artcod.equals(other.artcod)) {
            return false;
        }
        return true;
    }

    public int getCantidadDisponible() {
        return cantidadDisponible;
    }

    public void setCantidadDisponible(int cantidadDisponible) {
        this.cantidadDisponible = cantidadDisponible;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 71 * hash + this.nroPool;
        hash = 71 * hash + (this.nroCta != null ? this.nroCta.hashCode() : 0);
        hash = 71 * hash + this.orden;
        hash = 71 * hash + (this.tippro != null ? this.tippro.hashCode() : 0);
        hash = 71 * hash + (this.artcod != null ? this.artcod.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "ItemReserva{" + "nroPool=" + nroPool + "nroCta=" + nroCta + "orden=" + orden + "tippro=" + tippro + "artcod=" + artcod + '}';
    }

    

    
}
